//
//  RadioViewController.m
//  项目
//
//  Created by 杨世博 on 15-1-5.
//  Copyright (c) 2015年 张诗雨. All rights reserved.
//

#import "RadioViewController.h"
//获取屏幕尺寸的宏
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "UIImageView+WebCache.h"
#import "AFNetWoringYu.h"
#import "MJRefresh.h"
#import "WebColor.h"
#import "MJRefreshHeaderView.h"
#import "MJRefreshFooterView.h"
#import "RadioModel.h"
#import "RadioTableViewCell.h"
#import "RadioDetailViewController.h"
@interface RadioViewController ()<UITableViewDataSource,UITableViewDelegate,SDWebImageManagerDelegate>

@end

@implementation RadioViewController
- (void)dealloc
{
    [_tableView release];
    [_tableArray release];
    [super dealloc];
    
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.frame = CGRectMake(0, 56, self.view.frame.size.width, SCREEN_HEIGHT - 56);
        self.tableArray = [NSMutableArray array];
        self.startCount = 10;
    }
    return self;
}
- (void)creatView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , SCREEN_HEIGHT - 56)];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"radioCell"];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [_tableView release];
}
//Cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"radioCell" forIndexPath:indexPath];
    RadioModel *model = [_tableArray objectAtIndex:indexPath.row];
    [cell.covringImage setImageWithURL:[NSURL URLWithString:model.baseing]];
    [cell.titleLabel setText:model.title];
    [cell.uNameLabel setText:[model.userinfo objectForKey:@""]];
    [cell.guideLabel setText:model.guide];
    if (model.isnew) {
        [cell.soundNew setImage:[UIImage imageNamed:@"soundNew.png"]];
    }
    
    return cell;
}
//点击该Cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RadioDetailViewController *detail = [[RadioDetailViewController alloc] init];
    RadioModel *model = [_tableArray objectAtIndex:indexPath.row];
    detail.radioid = model.radioid;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
}
//cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT / 3;
}



//请求数据

- (void)requestData
{
    [AFNetWoringYu netWorkWithURL:@"" body:@"" resultBlock:^(id result) {
        [AFNetWoringYu netWorkWithURL:@"" body:@"" resultBlock:^(id result) {
            NSDictionary *dic = [NSDictionary dictionaryWithDictionary:result];
            NSMutableArray *array = [[dic objectForKey:@"data"] objectForKey:@"list"];
            for (NSDictionary *myDic in array) {
                RadioModel *model = [[RadioModel alloc] init];
                [model setValuesForKeysWithDictionary:myDic];
                [self.tableArray addObject:model];
            }
            [self.tableView reloadData];
        }];
    }];
    
}
//下拉加载
- (void)pullDownLoad
{
    [self.tableView addHeaderWithCallback:^{
        [AFNetWoringYu netWorkWithURL:@"" body:@"" resultBlock:^(id result) {
            [self.tableArray removeAllObjects];
            NSDictionary *dic = [NSDictionary dictionaryWithDictionary:result];
            NSMutableArray *array = [[dic objectForKey:@"data"] objectForKey:@"list"];
            for (NSDictionary *myDic in array) {
                RadioModel *model = [[RadioModel alloc] init];
                [model setValuesForKeysWithDictionary:myDic];
                [self.tableArray addObject:model];
            }
            [self.tableView reloadData];
        }];
    }];
    
}
//上拉刷新
- (void)pullUpRefresh
{
    [self.tableView addFooterWithCallback:^{
        if (self.startCount < 1000) {
            [AFNetWoringYu netWorkWithURL:@"" body:@"" resultBlock:^(id result) {
                [self.tableArray removeAllObjects];
                NSDictionary *dic = [NSDictionary dictionaryWithDictionary:result];
                NSMutableArray *array = [[dic objectForKey:@"data"] objectForKey:@"list"];
                for (NSDictionary *myDic in array) {
                    RadioModel *model = [[RadioModel alloc] init];
                    [model setValuesForKeysWithDictionary:myDic];
                    [self.tableArray addObject:model];
                }
                [self.tableView reloadData];
                _startCount += 10;
                [self.tableView footerEndRefreshing];
            }];
            
        }
    }];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
